package pl.ws;

import java.util.List;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import pl.model.dao.BookDao;
import pl.model.domain.Book;
import pl.model.service.SearchService;
import pl.model.service.impl.SearchServiceImpl.SearchResult;

@WebService(endpointInterface="pl.model.service.SearchService")
public class BookWSImpl implements SearchService, ApplicationContextAware {
	
	protected final Log log = LogFactory.getLog(getClass());
	protected BookDao bookDao;
	protected SearchService searchService;
	protected ApplicationContext applicationContext;

	//
//	@Override
//	public String testSayHello(String name) throws Exception {
//		return searchService.testSayHello(name);
//	}
//
	
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public void delete(Book object) {
		bookDao.delete(object);
	}

	public Book find(int id) {
		return bookDao.find(id);
	}

	public List<Book> findByDescription(String description) {
		return bookDao.findByDescription(description);
	}

	public List<Book> findByTitle(String title) {
		return bookDao.findByTitle(title);
	}

	public Book findWithAuthor(Integer bookId) {
		return bookDao.findWithAuthor(bookId);
	}

	public void flush() {
		bookDao.flush();
	}

	public List<Book> getAll() {
		return bookDao.getAll();
	}

	public List<Book> getAllSortedByTags() {
		return bookDao.getAllSortedByTags();
	}

	public Book merge(Book object) {
		return bookDao.merge(object);
	}

	public void persist(Book object) {
		bookDao.persist(object);
	}

	public void remove(Book object) {
		bookDao.remove(object);
	}

	public void save(Book object) {
		bookDao.save(object);
	}

	public List<Book> searchBookByDescription(String[] keywords) {
		return searchService.searchBookByDescription(keywords);
	}

	public Book searchBookById(Integer id) {
		return searchService.searchBookById(id);
	}

	public Book searchBookByIdWithProjection(Integer id) {
		return searchService.searchBookByIdWithProjection(id);
	}

	public List<Book> searchBookByTags(String[] tags) {
		log.info("BookWSImpl.searchBookByTags");
		return searchService.searchBookByTags(tags);
	}

	public List<Book> searchBookByTitle(String title) {
		return searchService.searchBookByTitle(title);
	}

	public SearchResult searchBookUniversal(String search_type,
			String[] search_by, String keywords, String rank_value,
			String sort_by, Integer page_size, Integer page_no) {
		return searchService.searchBookUniversal(search_type, search_by,
				keywords, rank_value, sort_by, page_size, page_no);
	}

	public List<Book> searchBooksByRank(Integer min, Integer max) {
		return searchService.searchBooksByRank(min, max);
	}

//	public List<Book> searchBooksByTagsTitleDescription(String keywords) {
//		return searchService.searchBooksByTagsTitleDescription(keywords);
//	}

	public List<Book> searchBooksByTagsTitleDescription(String[] keywords) {
		List<Book> books = null;
		log.info("BooksWSImpl start" );
		log.info("keywords: " + keywords);
		log.info("keywords " + StringUtils.join(keywords, " "));
		log.info("searchService " + searchService);
		log.info("bookDao " + bookDao);
		log.info("applicationContext " + applicationContext);
		try {
			books = searchService.searchBooksByTagsTitleDescription(keywords);
		} catch (RuntimeException e) {
			log.info("blad: " + e.getMessage());
			e.printStackTrace();
		}
		return books;
	}

	public void testDao() {
		searchService.testDao();
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		log.info("setAppCtx start");
		this.applicationContext=applicationContext;
		this.bookDao = (BookDao) applicationContext.getBean("bookDao");
		this.searchService = (SearchService) applicationContext.getBean("searchService");

	}

}
